package com.leetcode.weekly.contest.no284th;

/**
 * @author 覃国强
 * @date 2022-03-13 10:57
 */
public class A03_K次操作后最大化顶端元素 {

  static
  class Solution {
    public int maximumTop(int[] nums, int k) {

      if (nums.length < 2) {
        return -1;
      }

      if (k < 3) {
        return k == nums.length ? -1 : nums[k];
      }

      int max = -1;
      int len = Math.min(k - 1, nums.length);
      for (int i = 0; i < len; ++i) {
        max = Math.max(nums[i], max);
      }

      if (k + 1 < nums.length) {
        max = Math.max(nums[k + 1], max);
      }

      return max;
    }
  }

}
